

from tkinter import *

root = Tk()

frame = Frame(root)

frame.pack(padx=10,pady=10)

v1 = StringVar()
v2 = StringVar()
v3 = StringVar()

def test(content):
    print(content)
    return content.isdigit()

testCMD = frame.register(test)

first =Entry(frame,textvariable=v1,
        validate="key",
        validatecommand=(testCMD,"%P"))
addtext = Label(frame,text="+")
second =Entry(frame,textvariable=v2,
        validate="key",
        validatecommand=(testCMD,"%P"))

eqaltext = Label(frame,text="=")
result= Entry(frame,textvariable=v3,
        state="readonly")


def getAddResult():
    
    v3.set(int(first.get())+int(second.get()))

btn = Button(frame,text="计算结果",command=getAddResult)

first.grid(row=0,column=0,padx=5,pady=5)
addtext.grid(row=0,column=1,padx=5,pady=5)
second.grid(row=0,column=2,padx=5,pady=5)
eqaltext.grid(row=0,column=3,padx=5,pady=5)
result.grid(row=0,column=4,padx=5,pady=5)

btn.grid(row=1,column=2,padx=5,pady=5)

mainloop()